﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Db4objects.Db4o;
using Db4objects.Db4o.Linq;
using Domain; 

namespace DAO
{
    public static class OperadorDAO
    {
        /*
        public static List<Operador> TraerTodos()
        {
            using (IObjectContainer db = Connect.getDataBase())
            {
                try
                {
                    List<Operador> lstO = (from Operador o in db
                                          select o).ToList<Operador>();

                    Connect.lstAssignId(db, lstO.AsQueryable().OfType<object>().ToList());
                    return lstO;
                }
                finally
                {
                    db.Close();
                }
            }
        }

        public static Operador Traer(string username)
        {
            using (IObjectContainer db = Connect.getDataBase())
            {
                try
                {
                    Operador o = new Operador();
                    o.UserName = username;
                    IObjectSet oc = db.QueryByExample(o);

                    if (!oc.HasNext())
                        return null;

                    o = (Operador)oc.Next();
                    Connect.assignId(db, (object)o);
                    return o;
                }
                finally
                {
                    db.Close();
                }
            }
        }

        public static bool Guardar(Operador operador)
        {
            using (IObjectContainer db = Connect.getDataBase())
            {
                try
                {
                    if (operador.IdCliente == 0)
                    {
                        System.Collections.Hashtable constrains = new System.Collections.Hashtable();
                        constrains.Add(Connect.GetPropertyName(() => cliente.IdEmpresa), cliente.IdEmpresa);
                        Cliente mc = (Cliente)Connect.getMax(db, cliente, Connect.GetPropertyName(() => cliente.IdCliente).ToString(), constrains);
                        cliente.IdCliente = (mc == null ? 0 : mc.IdCliente) + 1;
                    }
                    else
                    {
                        if (!cliente.db4oid.HasValue)
                            throw new Exception(string.Format("At {0} parametro {1} sin db4id", System.Reflection.MethodBase.GetCurrentMethod().Name, cliente.GetType()));

                        db.Ext().Bind(cliente, cliente.db4oid.Value);
                    }


                    db.Store(cliente);
                    db.Commit();
                }
                finally
                {
                    db.Close();
                }
            }
            return true;
        }
         */
    }
}
